/*************************************************************************
 * ADOBE CONFIDENTIAL
 * ___________________
 *
 * Copyright 2014 Adobe Inc.
 * All Rights Reserved.
 *
 * NOTICE: Adobe permits you to use, modify, and distribute this file in
 * accordance with the terms of the Adobe license agreement accompanying
 * it. If you have received this file from a source other than Adobe,
 * then your use, modification, or distribution of it requires the prior
 * written permission of Adobe.
 **************************************************************************/

/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global window, document, CSInterface*/

var themeManager = (function () {
    'use strict';

    /**
     * Convert the Color object to string in hexadecimal format;
     */
    function toHex(color, delta) {
        function computeValue(value, delta) {
            var computedValue = !isNaN(delta) ? value + delta : value;
            if (computedValue < 0) {
                computedValue = 0;
            } else if (computedValue > 255) {
                computedValue = 255;
            }

            computedValue = Math.round(computedValue).toString(16);
            return computedValue.length === 1 ? "0" + computedValue : computedValue;
        }

        var hex = "";
        if (color) {
            hex = computeValue(color.red, delta) + computeValue(color.green, delta) + computeValue(color.blue, delta);
        }
        return hex;
    }


    function reverseColor(color, delta) {
        return toHex({
                red: Math.abs(255 - color.red),
                green: Math.abs(255 - color.green),
                blue: Math.abs(255 - color.blue)
            },
            delta);
    }


    function addRule(stylesheetId, selector, rule) {
        var stylesheet = document.getElementById(stylesheetId);

        if (stylesheet) {
            stylesheet = stylesheet.sheet;
            if (stylesheet.addRule) {
                stylesheet.addRule(selector, rule);
            } else if (stylesheet.insertRule) {
                stylesheet.insertRule(selector + ' { ' + rule + ' }', stylesheet.cssRules.length);
            }
        }
    }



    /**
     * Update the theme with the AppSkinInfo retrieved from the host product.
     */
    function updateThemeWithAppSkinInfo(appSkinInfo) {

        var panelBackgroundColor = appSkinInfo.panelBackgroundColor.color;
        document.body.bgColor = toHex(panelBackgroundColor);

        var styleId = "hostStyle";

        addRule(styleId, ".hostFontSize", "font-size:" + appSkinInfo.baseFontSize + "px;");
        addRule(styleId, ".hostFontFamily", "font-family:" + appSkinInfo.baseFontFamily);
        addRule(styleId, ".hostFontColor", "color:" + "#" + reverseColor(panelBackgroundColor, 20));
        addRule(styleId, ".hostBgd", "background-color:" + "#" + toHex(panelBackgroundColor, 20));
        addRule(styleId, ".hostBorder", "border-color: " + "#" + toHex(panelBackgroundColor, -80));



        var gradientBg			= "background-image: -webkit-linear-gradient(top, " + toHex(panelBackgroundColor, 40) + " , " + toHex(panelBackgroundColor, 10) + ");";
        var gradientDisabledBg	= "background-image: -webkit-linear-gradient(top, " + toHex(panelBackgroundColor, 15) + " , " + toHex(panelBackgroundColor, 5) + ");";
        var boxShadow			= "-webkit-box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.4), 0 1px 1px rgba(0, 0, 0, 0.2);";
        var boxActiveShadow		= "-webkit-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.6);";

        var isPanelThemeLight	= panelBackgroundColor.red > 100; // choose your own sweet spot
        var fontColor, disabledFontColor;
        var borderColor;
        var inputBackgroundColor;
        var gradientHighlightBg;


        var trackBg;
        var selectBg;
        var thumbBg;

        if(isPanelThemeLight) {
            fontColor				= "#000000;";
            disabledFontColor		= "color:" + toHex(panelBackgroundColor, -70) + ";";
            borderColor				= "border-color: " + toHex(panelBackgroundColor, -90) + ";";
            inputBackgroundColor	= toHex(panelBackgroundColor, 54) + ";";
            gradientHighlightBg		= "background-image: -webkit-linear-gradient(top, " + toHex(panelBackgroundColor, -40) + " , " + toHex(panelBackgroundColor,-50) + ");";


            trackBg = "background: #9b9b9b;";
            // selectBg = "background: #cbcbcb;";
            selectBg = trackBg;
            thumbBg = "background: #f1f1f1;";

        } else {
            fontColor				= "#ffffff;";
            disabledFontColor		= "color:" + toHex(panelBackgroundColor, 100) + ";";
            borderColor				= "border-color: " + toHex(panelBackgroundColor, -45) + ";";
            inputBackgroundColor	= toHex(panelBackgroundColor, -20) + ";";
            gradientHighlightBg		= "background-image: -webkit-linear-gradient(top, " + toHex(panelBackgroundColor, -20) + " , " + toHex(panelBackgroundColor, -30) + ");";

            trackBg = "background: #7b7b7b;";
            // selectBg = "background: #cbcbcb;";
            selectBg = trackBg;
            thumbBg = "background: #e1e1e1;";
        }

        //Update the default text style with pp values

        addRule(styleId, ".default", "font-size:" + appSkinInfo.baseFontSize + "px" + "; color:" + fontColor + "; background-color:" + toHex(panelBackgroundColor) + ";");
        addRule(styleId, "button, select, input[type=text], input[type=button], input[type=submit]", borderColor);
        addRule(styleId, "p", "color:" + fontColor + ";");
        addRule(styleId, "span", "color:" + fontColor + ";");
        addRule(styleId, "h1", "color:" + fontColor + ";");
        addRule(styleId, "h2", "color:" + fontColor + ";");
        addRule(styleId, "button", "font-family: " + appSkinInfo.baseFontFamily + ", Arial, sans-serif;");
        addRule(styleId, "button", "color:" + fontColor + ";");
        addRule(styleId, "button", "font-size:" + (1.2 * appSkinInfo.baseFontSize) + "px;");
        addRule(styleId, "button, select, input[type=button], input[type=submit]", gradientBg);
        addRule(styleId, "button, select, input[type=button], input[type=submit]", boxShadow);
        addRule(styleId, "button:enabled:active, input[type=button]:enabled:active, input[type=submit]:enabled:active", gradientHighlightBg);
        addRule(styleId, "button:enabled:active, input[type=button]:enabled:active, input[type=submit]:enabled:active", boxActiveShadow);
        addRule(styleId, "[disabled]", gradientDisabledBg);
        addRule(styleId, "[disabled]", disabledFontColor);
        addRule(styleId, "input[type=text]", "padding:1px 3px;");
        addRule(styleId, "input[type=text]", "background-color: " + inputBackgroundColor + ";");
        addRule(styleId, "input[type=text]:focus", "background-color: #ffffff;");
        addRule(styleId, "input[type=text]:focus", "color: #000000;");


        addRule(styleId, "#brush-slider-data .slider-track-high", trackBg);
        addRule(styleId, "#brush-slider-data  .slider-selection", selectBg);
        addRule(styleId, "#brush-slider-data  .slider-handle", thumbBg);
    }


    function onAppThemeColorChanged(event) {
        var skinInfo = JSON.parse(window.__adobe_cep__.getHostEnvironment()).appSkinInfo;
        updateThemeWithAppSkinInfo(skinInfo);
    }


    function init() {

        var csInterface = new CSInterface();

        updateThemeWithAppSkinInfo(csInterface.hostEnvironment.appSkinInfo);

        csInterface.addEventListener(CSInterface.THEME_COLOR_CHANGED_EVENT, onAppThemeColorChanged);
    }

    return {
        init: init
    };

}());
